package cn.cjlmonster.demo02.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import cn.cjlmonster.demo02.R;

/**
 * cn.cjlmonster.demo02.dialog
 *
 * @author cjl
 * @project Demo02
 * @date 2022/8/24 15:44
 */
public class CalendarDialog extends Dialog {

    public static void showDialog(Context context) {
        CalendarDialog dialog = new CalendarDialog(context);
        dialog.show();
    }

    private CalendarDialog(@NonNull Context context) {
        super(context, R.style.BaseDialog);
        setContentView(R.layout.dialog_calendar);
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getAttributes().gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        initView();
    }

    private ImageButton ib_pre;
    private TextView tv_current_date;
    private ImageButton ib_next;
    private CalendarView calendarView;

    private void initView() {
        findViewById(R.id.btn_cancel).setOnClickListener(v -> dismiss());
        ib_pre = findViewById(R.id.ib_pre);
        tv_current_date = findViewById(R.id.tv_current_date);
        ib_next = findViewById(R.id.ib_next);
        calendarView = findViewById(R.id.calendarView);
        ib_next.setOnClickListener(v -> updateMask());
    }

    public void updateMask() {
        Calendar calendar = calendarView.getSelectedCalendar();
        int year = calendar.getYear();
        int month = calendar.getMonth();
        int day = calendar.getDay();

        Calendar c1 = new Calendar();
        c1.setYear(year);
        c1.setMonth(month);
        c1.setDay(day - 3);
        calendarView.addSchemeDate(c1);

        Calendar c2 = new Calendar();
        c2.setYear(year);
        c2.setMonth(month);
        c2.setDay(day - 4);
        calendarView.addSchemeDate(c2);

        Calendar c3 = new Calendar();
        c3.setYear(year);
        c3.setMonth(month);
        c3.setDay(day - 5);
        calendarView.addSchemeDate(c3);

        Calendar c4 = new Calendar();
        c4.setYear(year);
        c4.setMonth(month);
        c4.setDay(day - 7);
        calendarView.addSchemeDate(c4);
    }
}
